﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using FastReflectionLib;
using JSZS.API;
using Newtonsoft.Json;
using JSZS.Data;
using System.Data;
using Newtonsoft.Json.Linq;
using System.Net;
using System.Configuration;
using System.IO;

using System.Xml;
using System.Threading.Tasks;
using Newtonsoft.Json.Converters;

namespace JSZS.API
{
    public class Commanage : IWsService
    {
        public void ProcessRequest(HttpContext context, ref Msg_Result msg, string P1, string P2, QJY_User UserInfo)
        {
            MethodInfo methodInfo = typeof(Commanage).GetMethod(msg.Action.ToUpper());
            Commanage model = new Commanage();
            methodInfo.FastInvoke(model, new object[] { context, msg, P1, P2, UserInfo });
        }


        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">用户名</param>
        /// <param name="P2">密码（未解码的）</param>
        /// <param name="UserInfo"></param>
        public void LOGIN(HttpContext context, Msg_Result msg, string P1, string P2, QJY_User UserInfo)
        {

            string password = context.Request["password"];
            string username = context.Request["UserName"];
           password = CommonHelp.GetMD5(password);
            QJY_User userInfo = new QJY_User();
            List<QJY_User> userList = new QJY_UserB().GetEntities(d => d.Phone == username && d.Pasword == password).ToList();
            if (userList.Count() == 0)
            {
                msg.ErrorMsg = "用户名或密码不正确";
                return;
            }
            else
                userInfo = userList[0];

            if (userInfo == null)
            {
                msg.ErrorMsg = "用户名或密码不正确";
            }
            else
            {
                if (string.IsNullOrEmpty(userInfo.pccode))
                {
                    userInfo.pccode = System.Guid.NewGuid().ToString();
                }
                new QJY_UserB().Update(userInfo);
                msg.Result = userInfo.pccode;
                msg.Result1 = userInfo.ID;

                //LOG异步
                try
                {
                    Task.Factory.StartNew<string>(() =>
                    {
                        new QJY_LOGB().Insert(new QJY_LOG()
                        {
                            LogType = "PCLOGIN",
                            LogContent = "用户" + userInfo.TeacherName + "登录，登录时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 登录地点:" + CommonHelp.getIpAddr(),
                            CRUser = userInfo.ID.ToString(),
                            CRDate = DateTime.Now
                        });
                        return "";
                    });


                }
                catch { }
            }
        }
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void REGISTER(HttpContext context, Msg_Result msg, string P1, string P2, QJY_User UserInfo)
        {

            JObject tt = (JObject)JsonConvert.DeserializeObject(P1);
            string teacherName = tt["TeacherName"].ToString();
            if (teacherName == "")
            {
                msg.ErrorMsg = "姓名不能为空!";
            }
            string mobile = tt["Phone"].ToString();
            var userlist = new QJY_UserB().GetEntities(p => p.Phone == mobile);
            if (userlist.Count() > 0)
            {
                msg.ErrorMsg = "此手机已注册用户";
            }
            if (msg.ErrorMsg == "")
            {
                QJY_User user = tt.ToObject<QJY_User>();
                user.CRDate = DateTime.Now;
                user.CRUser = "";
                string password = user.Pasword;
                user.Pasword = CommonHelp.GetMD5(password);
                new QJY_UserB().Insert(user);
                msg.Result = user;
                string content = "您的教师助手账号已注册成功，信息如下：\r\n登录账号：" + user.TeacherName + "\r\n密码：" + password + "\r\n,如非本人操作，请忽略本短信";
                CommonHelp.SendDX(user.Phone, content, "");
            }
        }
        public void CHECKREGISTERPHONE(HttpContext context, Msg_Result msg, string P1, string P2, QJY_User UserInfo)
        {
            var userList = new QJY_UserB().GetEntities(p => p.Phone == P1.Trim());
            if (userList.Count() > 0)
            {
                msg.ErrorMsg = "此手机已注册用户，请更换手机号继续注册";
            }
        }

        /// <summary>
        /// 发送手机验证码
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void SENDCHKMSG(HttpContext context, Msg_Result msg, string P1, string P2, QJY_User UserInfo)
        {
            if (!string.IsNullOrEmpty(P1))
            {
                string code = CommonHelp.numcode(4);
                try
                {
                    string type = context.Request["type"] ?? "";
                    string content = "";
                    switch (type)
                    {
                        default:
                            content = "教师助手注册验证码为：" + code + "，如非本人操作，请忽略本短信";
                            break;
                    }
                    CommonHelp.SendDX(P1, content, "");
                    msg.Result = CommonHelp.GetMD5(code);
                }
                catch
                {
                    msg.ErrorMsg = "发送验证码失败";
                }
            }
        }
    }
}